package com.itheima.sh.test;

import com.itheima.sh.dao.UserMapper;
import com.itheima.sh.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MybatisTest01 {
    private static UserMapper mapper = null;

    @BeforeClass
    public static void beforeClass() throws Exception {
        //1.创建工厂创造类对象
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //2.获取工厂对象
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sessionFactory = builder.build(is);
        //3.创建会话对象
        SqlSession sqlSession = sessionFactory.openSession(true);
        //4.获取接口的动态代理对象
        mapper = sqlSession.getMapper(UserMapper.class);
    }

   /*
        User queryUserById(@Param("id")Long id);
    */
    @Test
    public void queryUserById(){
        User user = mapper.queryUserById(1L);
        System.out.println("user = " + user);
    }
    /*
        void addUser(User user);
     */
    @Test
    public void addUser(){
        //创建对象
        User user = new User();
        user.setAge(18);
        user.setUserName("岩岩");
        user.setSex(0);
        user.setName("柳岩");
        user.setPassword("1234");
        mapper.addUser(user);
    }
    /*
        void updateUser(User user);
     */
    @Test
    public void updateUser(){
        //创建对象
        User user = new User();
        user.setAge(19);
        user.setUserName("冰冰");
        user.setSex(0);
        user.setName("范冰冰");
        user.setPassword("4567");
        user.setId(11L);
        mapper.updateUser(user);
    }
    /*
        void deleteUserById(@Param("id")Long id);
     */
    @Test
    public void deleteUserById(){
        mapper.deleteUserById(12L);
    }

    /*
        List<User> queryUserBySexOrUsername(@Param("userName")String userName);
     */
    @Test
    public void queryUserBySexOrUsername(){
        List<User> list = mapper.queryUserBySexOrUsername("zhang");
        System.out.println("list = " + list);
    }
    /*
         User queryUserById2(@Param("id")Long id);
     */
    @Test
    public void queryUserById2(){
        User user = mapper.queryUserById2(1L);
        System.out.println("user = " + user);
    }
}
